using System;
using Game2DFramework;
using Game2DFramework.Collision;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.Game.Particles
{
    class Projectile : GameObject 
    {
        public float Rotation;
        public Vector2 Position;
        public float Speed;
        public Vector2 Direction;
        public bool Deleted;
        public int Damage;
        public Texture2D Texture;
        
        public Circle Bounds
        {
            get { return new Circle((int) Position.X, (int) Position.Y, 4); }
        }

        public Projectile(Game2D game, Vector2 position, float speed, Vector2 direction, int damage) : base(game)
        {
            Position = position;
            Speed = speed;
            Direction = direction;
            Rotation = (float) Math.Atan2(Direction.Y, Direction.X) + MathHelper.PiOver2;
            Damage = damage;
        }
        
        public virtual void Update(float elapsed)
        {
            if (Deleted) return;

            Position += Direction*Speed*elapsed;
            if (Position.X < 0 || Position.X > Game.ScreenWidth || Position.Y < 0 || Position.Y > Game.ScreenHeight) Deleted = true;
        }

        public override void OnCollide(GameObject other)
        {
            Deleted = true;
            other.ApplyDamage(Damage);
        }

        public virtual bool ReadyToBeRemoved {get { return Deleted; }}

        public virtual void Draw()
        {
            if (Deleted) return;
            Game.SpriteBatch.Draw(Texture, Position, null, Color.White, Rotation,
                                  new Vector2(Texture.Width, Texture.Height) * 0.5f, 1.0f, SpriteEffects.None, 0);
        } 
    }
}